﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;

using Warehouse.Service.Data;
using Warehouse.Silverlight.Core.Infrastructure;
using Warehouse.Silverlight.Core.Infrastructure.ViewModels;
using Warehouse.Silverlight.Modules.Main.Models;
using Warehouse.Silverlight.Modules.Main.Services;

namespace Warehouse.Silverlight.Modules.Main.ViewModels
{
    [Export]
    public class TestEditorViewModel : ViewModel
    {
        private readonly IProductService _productService;
        private readonly ICategoryService _categoryService;
        private readonly IEventAggregator _eventAggregator;
        private ObservableCollection<Category> _categoryCollection;
        private Product _current;

        [ImportingConstructor]
        public TestEditorViewModel(IProductService productService, ICategoryService categoryService, IEventAggregator eventAggregator)
        {
            _productService = productService;
            _categoryService = categoryService;
            _eventAggregator = eventAggregator;

            Initialize();
        }

        public Product Current
        {
            get { return _current; }
            set
            {
                if (_current != value)
                {
                    _current = value;
                    RaisePropertyChanged(() => Current);
                }
            }
        }

        public ObservableCollection<Category> CategoryCollection
        {
            get { return _categoryCollection; }
        }

        // Override
        protected override void Initialize()
        {
            _eventAggregator.GetEvent<SelectedProductChangedEvent>().Subscribe(ChangeCurrent);

            _categoryCollection = _categoryService.GetAllCategories();
        }

        #region Private Helpers

        public void ChangeCurrent(int newId)
        {
            this.Current = _productService.GetProduct(newId);
        }

        #endregion
    }
}
